<?php
/*
+-----------------------------------------------------------------------------+
| $Id: function.ajax.php 2009-10-12 11:20:41Z Bleakwind $
| Global ajax function
| Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
| http://www.weaverdream.com/
| Release under the GNU Lesser General Public License Version 3 (LGPLv3):
|   http://www.gnu.org/licenses/lgpl.html
+-----------------------------------------------------------------------------+
*/

function set_value($handle, $target="", $data="", $attribute="innerHTML")
{
    $ajax_response = new xajaxResponse();
    if($handle == "assign"){
        $ajax_response->assign($target, $attribute, $data);
    }elseif($handle == "append"){
        $ajax_response->append($target, $attribute, $data);
    }elseif($handle == "remove"){
        $ajax_response->remove($target);
    }elseif($handle == "script"){
        $ajax_response->script($data);
    }
    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "set_value");

function alert_msg($msg, $url="")
{
    $ajax_response = new xajaxResponse();
    $ajax_response->alert($msg);
    if($url == ""){
        $ajax_response->script("history.back(1);");
    }else{
        $ajax_response->redirect($url);
    }
    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "alert_msg");

function delete_record($table,$condition)
{
    global $CONFIGURE;
    $ajax_response = new xajaxResponse();
    $db_delete_record = &ADONewConnection(CONFIG_DB_TYPE);
    $db_delete_record->NConnect(CONFIG_DB_HOSTNAME, CONFIG_DB_USERNAME, CONFIG_DB_PASSWORD, CONFIG_DB_DATABASE);
    $db_delete_record->Execute("SET NAMES '".$CONFIGURE['database']['charset']."'");
    $sql_delete_record = "DELETE FROM ".$table." WHERE ".$condition;
    $db_delete_record->Execute($sql_delete_record);
    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "delete_record");

function switch_verifycode($id, $dir, $name)
{
    $ajax_response = new xajaxResponse();

    $ajax_response->assign($id, "innerHTML", "<img src=\"".$dir."?act=verifycode&vc=".$name."&rand=".md5(microtime())."\" border=\"0\" align=\"absmiddle\">");

    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "switch_verifycode");

// Display table
function bwbbcode_code_js($id, $value = "")
{
    global $LANGUAGE;
    $ajax_response = new xajaxResponse();
    $value = preg_replace("/\<ul\>\n?|\<\/ul\>\n?|\<li\>|\<\/li\>|\<span style\=\"color\: \#[0-9A-F]{6}\"\>|\<span style\=\"color\: rgb\([0-9]{1,3}\, [0-9]{1,3}\, [0-9]{1,3}\)\;\"\>|\<\/span\>/i", "", $value);
    $value = str_replace(array("&nbsp;"), array(" "), htmlspecialchars_decode(trim($value), ENT_QUOTES));
    $ajax_response->assign($id, "value", $value);
    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "bwbbcode_code_js");
//////
?>